from django.db import models
from login.models import UserModel
import uuid

class ProductCategory(models.Model):
    """商品分类"""
    name = models.CharField(max_length=100, verbose_name='分类名称')
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, verbose_name='父分类')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    # 添加保险金融特有的分类属性
    is_financial = models.BooleanField(default=False, verbose_name='金融产品')
    icon = models.CharField(max_length=100, null=True, blank=True, verbose_name='分类图标')

    class Meta:
        db_table = 'product_category'
        verbose_name = '商品分类'
        verbose_name_plural = '商品分类'

    def __str__(self):
        return self.name

class Product(models.Model):
    """商品/保险金融产品"""
    name = models.CharField(max_length=200, verbose_name='产品名称')
    code = models.CharField(max_length=50, unique=True, verbose_name='产品代码')
    category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE, verbose_name='产品分类')
    description = models.TextField(verbose_name='产品描述')
    # 保险金融产品特有字段
    premium = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='保费', null=True, blank=True)
    coverage_amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='保额', null=True, blank=True)
    coverage_period = models.CharField(max_length=50, verbose_name='保障期限', null=True, blank=True)
    target_audience = models.CharField(max_length=100, verbose_name='目标人群', null=True, blank=True)
    # 通用字段
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    stock = models.IntegerField(default=0, verbose_name='库存/限额')
    is_active = models.BooleanField(default=True, verbose_name='是否上架')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    # 保险金融产品特性
    features = models.TextField(verbose_name='产品特点', null=True, blank=True)
    claim_process = models.TextField(verbose_name='理赔流程', null=True, blank=True)
    # 金融产品特有字段
    annual_return = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='年化收益率', null=True, blank=True)
    risk_level = models.CharField(max_length=20, choices=[
        ('low', '低风险'),
        ('medium', '中风险'),
        ('high', '高风险')
    ], default='medium', verbose_name='风险等级')


    img_url=models.CharField(max_length=126, verbose_name='产品图片', null=True, blank=True)

    class Meta:
        db_table = 'product'
        verbose_name = '保险金融产品'
        verbose_name_plural = '保险金融产品'

    def __str__(self):
        return self.name

class ProductReview(models.Model):
    """产品评价"""
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='reviews', verbose_name='产品')
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='用户')
    rating = models.IntegerField(default=5, verbose_name='评分')
    content = models.TextField(verbose_name='评价内容')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='评价时间')
    # 添加保险金融特有的评价属性
    claim_experience = models.TextField(verbose_name='理赔体验', null=True, blank=True)
    service_rating = models.IntegerField(default=5, verbose_name='服务评分')

    class Meta:
        db_table = 'product_review'
        verbose_name = '产品评价'
        verbose_name_plural = '产品评价'
        unique_together = ['product', 'user']


from django.db import models

class Banner(models.Model):
    title = models.CharField(max_length=100, verbose_name='标题')
    image = models.ImageField(upload_to='banners/', verbose_name='图片')
    link = models.URLField(blank=True, null=True, verbose_name='跳转链接')
    order = models.IntegerField(default=0, verbose_name='排序')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    desc=models.CharField(max_length=100, verbose_name='描述',blank=True,null=True)
    class Meta:
        db_table = 'banner'
        ordering = ['order', '-created_at']
    def __str__(self):
        return self.title


class HistoryModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='用户')
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name='商品')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    number=models.IntegerField(default=1,verbose_name='数量')
    type=models.ForeignKey(ProductCategory, on_delete=models.CASCADE, verbose_name='商品类型')
    class Meta:
        db_table = 'history'
